<template>
  <div class="recent-activity">
    <div class="activity-header">
      <h3>最新活动</h3>
      <el-button type="text" @click="refreshActivities" :loading="loading">
        <el-icon><Refresh /></el-icon>
        刷新
      </el-button>
    </div>
    
    <div class="activity-list" v-loading="loading">
      <div v-if="activities.length === 0" class="empty-state">
        <el-icon :size="48" color="#c0c4cc"><Document /></el-icon>
        <p>暂无活动记录</p>
      </div>
      
      <div v-else class="activity-items">
        <div 
          v-for="activity in activities" 
          :key="activity.id" 
          class="activity-item"
        >
          <div class="activity-icon">
            <el-icon :size="16" :color="getActivityIconColor(activity.logType)">
              <component :is="getActivityIcon(activity.logType)" />
            </el-icon>
          </div>
          <div class="activity-content">
            <div class="activity-title">{{ activity.action }}</div>
            <div class="activity-description">{{ activity.description }}</div>
            <div class="activity-meta">
              <span class="activity-time">{{ formatTime(activity.createTime) }}</span>
              <span class="activity-ip">{{ activity.ipAddress }}</span>
            </div>
          </div>
          <div class="activity-status">
            <el-tag 
              :type="activity.status === 'SUCCESS' ? 'success' : 'danger'" 
              size="small"
            >
              {{ activity.status === 'SUCCESS' ? '成功' : '失败' }}
            </el-tag>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Refresh, Document, Monitor, Warning, DataAnalysis } from '@element-plus/icons-vue'
import { getRecentLogs } from '@/api/log'

const activities = ref([])
const loading = ref(false)

// 获取最近活动
const fetchRecentActivities = async () => {
  loading.value = true
  try {
    const response = await getRecentLogs(4)
    if (response.code === 200) {
      activities.value = response.data || []
    } else {
      // 如果是权限不足，不显示错误消息，只显示空状态
      if (response.code === 403) {
        activities.value = []
      } else {
        ElMessage.error(response.message || '获取活动记录失败')
      }
    }
  } catch (error) {
    console.error('获取最近活动失败:', error)
    // 如果是403错误，不显示错误消息，只显示空状态
    if (error.response && error.response.status === 403) {
      activities.value = []
    } else {
      ElMessage.error('获取活动记录失败')
    }
  } finally {
    loading.value = false
  }
}

// 刷新活动
const refreshActivities = () => {
  fetchRecentActivities()
}

// 格式化时间
const formatTime = (time) => {
  if (!time) return ''
  const date = new Date(time)
  const now = new Date()
  const diff = now - date
  
  // 小于1分钟
  if (diff < 60000) {
    return '刚刚'
  }
  // 小于1小时
  if (diff < 3600000) {
    return `${Math.floor(diff / 60000)}分钟前`
  }
  // 小于24小时
  if (diff < 86400000) {
    return `${Math.floor(diff / 3600000)}小时前`
  }
  // 大于24小时
  return date.toLocaleDateString('zh-CN')
}

// 获取活动图标
const getActivityIcon = (logType) => {
  const icons = {
    'LOGIN': Monitor,
    'OPERATION': Document,
    'DATA': DataAnalysis,
    'ERROR': Warning
  }
  return icons[logType] || Document
}

// 获取活动图标颜色
const getActivityIconColor = (logType) => {
  const colors = {
    'LOGIN': '#409eff',
    'OPERATION': '#67c23a',
    'DATA': '#e6a23c',
    'ERROR': '#f56c6c'
  }
  return colors[logType] || '#909399'
}

// 组件挂载时获取数据
onMounted(() => {
  fetchRecentActivities()
})
</script>

<style scoped>
.recent-activity {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.activity-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.activity-header h3 {
  margin: 0;
  color: var(--text-color);
  font-size: 16px;
  font-weight: 600;
}

.activity-list {
  min-height: 200px;
}

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: #c0c4cc;
}

.empty-state p {
  margin: 16px 0 0 0;
  font-size: 14px;
}

.activity-items {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.activity-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 12px;
  border-radius: 6px;
  background: #f8f9fa;
  transition: all 0.3s ease;
}

.activity-item:hover {
  background: #f0f2f5;
}

.activity-icon {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: white;
  border-radius: 6px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.activity-content {
  flex: 1;
  min-width: 0;
}

.activity-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-color);
  margin-bottom: 4px;
}

.activity-description {
  font-size: 12px;
  color: var(--text-color-secondary);
  margin-bottom: 8px;
  line-height: 1.4;
}

.activity-meta {
  display: flex;
  gap: 12px;
  font-size: 11px;
  color: #909399;
}

.activity-time {
  white-space: nowrap;
}

.activity-ip {
  white-space: nowrap;
}

.activity-status {
  flex-shrink: 0;
}

@media (max-width: 768px) {
  .recent-activity {
    padding: 16px;
  }
  
  .activity-item {
    padding: 10px;
  }
  
  .activity-meta {
    flex-direction: column;
    gap: 4px;
  }
}

body.dark-theme .recent-activity {
  background: #181c20 !important;
  color: #fff !important;
  border-radius: 12px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.32);
  border: 1.5px solid #23272e;
}
body.dark-theme .recent-activity .activity-header h3 {
  color: #fff !important;
}
body.dark-theme .recent-activity .activity-header .el-button {
  background: #409eff !important;
  color: #fff !important;
  border-radius: 8px;
  border: none !important;
  box-shadow: 0 2px 8px rgba(64,158,255,0.15);
  transition: background 0.2s;
  font-size: 18px !important;
  padding: 8px 22px !important;
  height: 40px !important;
  display: flex;
  align-items: center;
}
body.dark-theme .recent-activity .activity-header .el-button .el-icon {
  font-size: 22px !important;
  margin-right: 6px;
}
body.dark-theme .recent-activity .activity-header .el-button:hover {
  background: #66b1ff !important;
  color: #fff !important;
}
body.dark-theme .recent-activity .activity-header .el-icon {
  color: #fff !important;
}
body.dark-theme .recent-activity .activity-list {
  min-height: 200px;
}
body.dark-theme .recent-activity .activity-items {
  gap: 16px;
}
body.dark-theme .recent-activity .activity-item {
  background: #23272e !important;
  border-radius: 8px;
  color: #fff !important;
  border: 1px solid #333;
  transition: background 0.3s;
}
body.dark-theme .recent-activity .activity-item:hover {
  background: #222 !important;
}
body.dark-theme .recent-activity .activity-icon {
  background: #181c20 !important;
  box-shadow: none;
  border-radius: 8px;
}
body.dark-theme .recent-activity .activity-title {
  color: #fff !important;
}
body.dark-theme .recent-activity .activity-description {
  color: #aaa !important;
}
body.dark-theme .recent-activity .activity-meta {
  color: #909399 !important;
}
body.dark-theme .recent-activity .activity-status .el-tag {
  background: #181c20 !important;
  color: #40a9ff !important;
  border: 1px solid #40a9ff !important;
}
body.dark-theme .recent-activity .empty-state {
  color: #666 !important;
}
</style> 